home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / HUFFTAB.ICN < prev    next >
Text File  |  1992-11-26  |  2KB  |  85 lines

  1. ############################################################################
  2. #
  3. #    File:     hufftab.icn
  4. #
  5. #    Subject:  Program to compute Huffman state transitions 
  6. #
  7. #    Author:   Gregg M. Townsend
  8. #
  9. #    Date:     August 21, 1992
  10. #
  11. ###########################################################################
  12. #
  13. #      Each input line should be a string of 0s & 1s followed by a value
  14. #   field.  Output is a list of items in a form suitable for inclusion
  15. #   by a C program as initialization for an array.  Each pair of items
  16. #   indicates the action to be taken on receipt of a 0 or 1 bit from the
  17. #   corresponding state; this is either a state number if more decoding
  18. #   is needed or the value field from the input if not.  State 0 is the
  19. #   initial state;  0 is output only for undefined states.  States are
  20. #   numbered by two to facilitate use of a one-dimensional array.
  21. #
  22. #   sample input:        corresponding output:
  23. #    00 a                /*  0 */  2, c, a, 4, 0, b,
  24. #    011 b
  25. #    1 c            [new line started every 10 entries]
  26. #
  27. #   Interpretation:
  28. #    from state 0,  input=0 => go to state 2,  input=1 => return c
  29. #    from state 2,  input=0 => return a,  input=1 => go to state 4
  30. #    from state 4,  input=0 => undefined,  input=1 => return b
  31. #
  32. ############################################################################
  33.  
  34. global curstate, sttab, line
  35.  
  36. procedure main()
  37.     local code, val, n
  38.  
  39.     sttab := list()
  40.     put(sttab)
  41.     put(sttab)
  42.     while line := read() do  {
  43.     line ? {
  44.         if ="#" | pos(0) then next
  45.         (code := tab(many('01'))) | (write(&errout,"bad: ",line) & next)
  46.         tab(many(' \t'))
  47.         val := tab(0)
  48.     }
  49.     curstate := 1
  50.     every bit(!code[1:-1])
  51.     curstate +:= code[-1]
  52.     if \sttab[curstate] then write(&errout,"dupl: ",line)
  53.     sttab[curstate] := val
  54.     }
  55.     write("/* generated by machine -- do not edit! */")
  56.     write()
  57.     writes("/*  0 */")
  58.     out(sttab[1])
  59.     every n := 2 to *sttab do {
  60.     if n % 10 = 1 then writes("\n/* ",n-1," */")
  61.     out(sttab[n])
  62.     }
  63.     write()
  64.     end
  65.  
  66.  
  67. procedure bit (c)
  68.     curstate +:= c
  69.     if integer(sttab[curstate]) then {
  70.     curstate := sttab[curstate]
  71.     return
  72.     }
  73.     if type(sttab[curstate]) == "string" then write(&errout,"dupl: ",line)
  74.     curstate := sttab[curstate] := *sttab + 1
  75.     put(sttab)
  76.     put(sttab)
  77.     end
  78.  
  79.  
  80. procedure out(v)
  81.     if type(v) == "integer"
  82.     then writes(right(v-1,6),",")
  83.         else writes(right(\v | "0",6),",")
  84.     end
  85.